package org.looop.studio.noots;

import java.io.File;
import java.util.Date;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

public class Searcher {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Throwable {
		File indexDir = new File("\\home\\lucene\\index");
		String q = "ZooKeeper";
		search(indexDir, q);
	}

	private static void search(File indexDir, String q) throws Throwable {
		Directory fsDir = FSDirectory.getDirectory(indexDir, false);
		IndexSearcher is = new IndexSearcher(fsDir);
		Query query = QueryParser.parse(q, "contents", new StandardAnalyzer());
		long start = new Date().getTime();
		Hits hits = is.search(query);
		long end = new Date().getTime();
		System.out.println("Found " + hits.length() + " document in " + (end - start) + " milliseconds that match query " + q + ":");
		
		for(int i=0; i < hits.length(); i++){
			Document doc = hits.doc(i);
			System.out.println(doc.get(" filename"));
		}
	}

}
